$(document).ready(function(){
	$('#product_form').submit(function(){
		return false;
	});
	$('#category_level_1').change(function(){
		getCategoryLevel3();
	});
	$('#category_level_2').change(function(){
		getCategoryLevel3();
		getProductFit();
		getProductCrotch();
		getProductSize();
		getProductCode();
	});
	$('#category_level_3').change(function(){
		getProductCode();
	});
	$('#product_fit').change(function(){
		getProductCode();
	});
	$('#product_crotch').change(function(){
		getProductCode();
	});
	$('#type_of_size').change(function(){
		getProductSize();
	});
	$('#btn_add_product').click(function(){
		addProduct();
	});
	$('#btn_update_product').click(function(){
		updateProduct();
	});
	$('#btn_delete_product').click(function(){
		deleteProduct();
	});
	$('#btn_clear_product').click(function(){
		$('#product_detail_id').val(0);
		$('#category_level_1').val(0);
		$('#category_level_2').val(0);
		$('#category_level_3').html('');
		$('#product_fit').html('');
		$('#product_crotch').html('');
		$('#type_of_size').val(0);
		$('#product_size').html('');
		$('#vi_name').val('');
		$('#en_name').val('');
		$('#retail_price').val('');
		$('#wholesale_price').val('');
		$('#description').val('');
		$('#product_custom_code').val('');
		$('#submit_error').html('');
		$('#search_product_code').val('');
		$('#search_product_custom_code').val('');
	});
	$('#btn_search_product').click(function(){
		getProduct();
	});
});

function getCategoryLevel3(callback){
	var category_level_1 = $('#category_level_1').val();
	var category_level_2 = $('#category_level_2').val();
	if(category_level_1 != 0 && category_level_2 != 0){
		var data = {
			category_level_1_id: category_level_1,
			category_level_2_id: category_level_2
		};
		$.ajax({
			'url' : base_url + GET_MAP_CATEGORY_3,
			'type' : 'POST',
			'data' : data,
			'success' : function(data){
				var category_level_3 = $('#category_level_3');
				category_level_3.html('');
				if(data){
					category_level_3.html(data);
					if(callback){
						callback();
					}
				}
			}
		});
	}
}

function getProductFit(callback){
	var category_level_2 = $('#category_level_2').val();
	$('#product_fit').html('');
	if(category_level_2 != 0){
		var data = {
			category_level_2_id: category_level_2
		};
		var request = {
			url: base_url + GET_LIST_PRODUCT_FIT,
			type: 'POST',
			data: data,
			success: function(data){
				if(data){
					$('#product_fit').html(data);
					if(callback){
						callback();
					}
				}
			},
			error: function(error){
			}
		};
		excuteAjax(request);
	}
}

function getProductCrotch(callback){
	var category_level_2 = $('#category_level_2').val();
	$('#product_crotch').html('');
	if(category_level_2 == 1){
		var request = {
			url: base_url + GET_LIST_PRODUCT_CROTCH,
			type: 'POST',
			success: function(data){
				if(data){
					$('#product_crotch').html(data);
					if(callback){
						callback();
					}
				}
			},
			error: function(error){
			}
		};
		excuteAjax(request);
	}
}

function getProductSize(callback){
	var category_level_2 = $('#category_level_2').val();
	var type_of_size = $('#type_of_size').val();
	$('#product_size').html('');
	if(category_level_2 != 0 && type_of_size != 0){
		var data = {
			category_level_2_id: category_level_2,
			type_of_size_id: type_of_size
		};
		var request = {
			url: base_url + '/product_size/get_list',
			type: 'POST',
			data: data,
			success: function(data){
				if(data){
					$('#product_size').html(data);
					if(callback){
						callback();
					}
				}
			},
			error: function(error){
			}
		};
		excuteAjax(request);
	}
}

function getProductCode(){
	var category_level_2 = $('#category_level_2').val();
	var category_level_3 = $('#category_level_3').val();
	var product_fit = $('#product_fit').val();
	var product_crotch = $('#product_crotch').val();
	$('#product_code').html('');
	if(category_level_2 != 0 && category_level_3 != 0){
		var data = {
			category_level_2_id: category_level_2,
			category_level_3_id: category_level_3,
			product_fit_id: product_fit,
			product_crotch_id: product_crotch
		};
		var request = {
			url: base_url + '/product_code/get_list',
			type: 'POST',
			data: data,
			success: function(data){
				if(data){
					$('#product_code').html(data);
				}
			},
			error: function(error){
			}
		};
		excuteAjax(request);
	}
}

function getProduct(){
	var search_product_code = $('#search_product_code').val();
	var search_product_custom_code = $('#search_product_custom_code').val();
	$('#submit_error').html('');
	if(search_product_code !== '' && search_product_custom_code !== ''){
		var data = {
			product_code_id: search_product_code,
			code: search_product_custom_code
		};
		var request = {
			url: base_url + GET_PRODUCT_DETAIL,
			type: 'POST',
			data: data,
			success: function(data){
				if(data){
					var product = JSON.parse(data);
					$('#product_detail_id').val(product.id);
					$('#category_level_1').val(product.category_level_1_id);
					$('#category_level_2').val(product.category_level_2_id);
					$('#type_of_size').val(product.type_of_size_id);
					$('#vi_name').val(product.vi_name);
					$('#en_name').val(product.en_name);
					$('#retail_price').val(product.retail_price);
					$('#wholesale_price').val(product.wholesale_price);
					$('#description').val(product.description);
					$('#product_custom_code').val(product.code);
					getCategoryLevel3(function(){
						$('#category_level_3').val(product.category_level_3_id);
					});
					getProductFit(function(){
						$('#product_fit').val(product.product_fit_id);
						getProductCode();
					});
					getProductCrotch(function(){
						$('#product_crotch').val(product.product_crotch_id);
						getProductCode();
					});
					getProductSize(function(){
						$('#product_size').val(product.product_size_id);
					});
				} else {
					$('#submit_error').html(RESPONSE_ERROR);
				}
			},
			error: function(error){
				$('#submit_error').html(RESPONSE_ERROR);
			}
		};
		excuteAjax(request);
	} else {
		$('#submit_error').html('Dữ liệu nhập vào không hợp lệ');
	}
	$("html, body").animate({ scrollTop: 0 }, "slow");
}

function addProduct(){
	var category_level_1 = $('#category_level_1').val();
	var category_level_2 = $('#category_level_2').val();
	var category_level_3 = $('#category_level_3').val();
	var product_fit = $('#product_fit').val();
	var product_crotch = $('#product_crotch').val();
	var product_size = $('#product_size').val();
	var vi_name = $('#vi_name').val();
	var en_name = $('#en_name').val();
	var retail_price = $('#retail_price').val();
	var wholesale_price = $('#wholesale_price').val();
	var description = $('#description').val();
	var product_custom_code = $('#product_custom_code').val();
	$('#submit_error').html('');
	if(category_level_1 != 0 && category_level_2 != 0 && category_level_3 != 0 &&
		product_size != 0 && product_custom_code !== '' &&
		vi_name !== '' && retail_price !=='' && wholesale_price !== ''){
		var data = {
			category_level_1_id: category_level_1,
			category_level_2_id: category_level_2,
			category_level_3_id: category_level_3,
			product_fit_id: product_fit,
			product_crotch_id: product_crotch,
			product_size_id: product_size,
			code: product_custom_code,
			vi_name: vi_name,
			en_name: en_name,
			retail_price: retail_price,
			wholesale_price: wholesale_price,
			description: description
		};
		var request = {
			url: base_url + '/product_detail/add',
			type: 'POST',
			data: data,
			success: function(data){
				if(data == '1'){
					$('#submit_error').html(RESPONSE_SUCCESS);
				} else if(data == 'exist'){
					$('#submit_error').html(RESPONSE_CODE_EXIST);
				} else {
					$('#submit_error').html(RESPONSE_ERROR);
				}
			},
			error: function(error){
				$('#submit_error').html(RESPONSE_ERROR);
			}
		};
		excuteAjax(request);
	} else {
		$('#submit_error').html('Dữ liệu nhập vào không hợp lệ');
	}
	$("html, body").animate({ scrollTop: 0 }, "slow");
}

function updateProduct(){
	var id = $('#product_detail_id').val();
	var category_level_1 = $('#category_level_1').val();
	var category_level_2 = $('#category_level_2').val();
	var category_level_3 = $('#category_level_3').val();
	var product_fit = $('#product_fit').val();
	var product_crotch = $('#product_crotch').val();
	var product_size = $('#product_size').val();
	var vi_name = $('#vi_name').val();
	var en_name = $('#en_name').val();
	var retail_price = $('#retail_price').val();
	var wholesale_price = $('#wholesale_price').val();
	var description = $('#description').val();
	var product_custom_code = $('#product_custom_code').val();
	$('#submit_error').html('');
	if(id != 0 && category_level_1 != 0 && category_level_2 != 0 && category_level_3 != 0 &&
		product_size != 0 && product_custom_code !== '' &&
		vi_name !== '' && retail_price !=='' && wholesale_price !== ''){
		var data = {
			id: id,
			category_level_1_id: category_level_1,
			category_level_2_id: category_level_2,
			category_level_3_id: category_level_3,
			product_fit_id: product_fit,
			product_crotch_id: product_crotch,
			product_size_id: product_size,
			code: product_custom_code,
			vi_name: vi_name,
			en_name: en_name,
			retail_price: retail_price,
			wholesale_price: wholesale_price,
			description: description
		};
		var request = {
			url: base_url + UPDATE_PRODUCT_DETAIL,
			type: 'POST',
			data: data,
			success: function(data){
				if(data == '1'){
					$('#submit_error').html(RESPONSE_SUCCESS);
				} else if(data == 'exist'){
					$('#submit_error').html(RESPONSE_CODE_EXIST);
				} else {
					$('#submit_error').html(RESPONSE_ERROR);
				}
			},
			error: function(error){
				$('#submit_error').html(RESPONSE_ERROR);
			}
		};
		excuteAjax(request);
	} else {
		$('#submit_error').html('Dữ liệu nhập vào không hợp lệ');
	}
	$("html, body").animate({ scrollTop: 0 }, "slow");
}

function deleteProduct(){
	var id = $('#product_detail_id').val();
	$('#submit_error').html('');
	if(id != 0){
		var data = {
			id: id
		};
		var request = {
			url: base_url + DELETE_PRODUCT_DETAIL,
			type: 'POST',
			data: data,
			success: function(data){
				if(data == '1'){
					$('#submit_error').html(RESPONSE_SUCCESS);
				} else {
					$('#submit_error').html(RESPONSE_ERROR);
				}
			},
			error: function(error){
				$('#submit_error').html(RESPONSE_ERROR);
			}
		};
		excuteAjax(request);
	} else {
		$('#submit_error').html('Dữ liệu nhập vào không hợp lệ');
	}
	$("html, body").animate({ scrollTop: 0 }, "slow");
}